package part2;

import java.awt.Graphics;
import java.awt.Image;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;

import javax.swing.ImageIcon;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.TransferHandler;


public class CardGame extends JFrame implements ActionListener, MouseListener {

	/**
	 * Data members
	 * - the serialVersionUID is needed to get rid of a compiler warning
	 * - 
	 */
	private static final long serialVersionUID = 1L;
	private static final String TITLE_STRING = "Drag and Drop Cards";
	private JLabel[] cards = new JLabel[3];
	private static final int[] xpos = { 50, 150, 250 };
	private static final int[] ypos = { 50,  50,  50 };
	private JButton   shuffle;
	
	public CardGame(){
		super( TITLE_STRING );
		setBounds(100, 100, 400, 300);
		setDefaultCloseOperation(EXIT_ON_CLOSE);
		setLayout( null );
		shuffle = new JButton("Cyle cards");
		shuffle.setBounds(125, 200, 150, 30 );
		shuffle.addActionListener( this );
		add( shuffle );
		
		cards[0] = new JLabel( new ImageIcon("src/cards/CardImages/01a.jpg") );
		cards[1] = new JLabel( new ImageIcon("src/cards/CardImages/01b.jpg") );
		cards[2] = new JLabel( new ImageIcon("src/cards/CardImages/01c.jpg") );
		
		int i = 0;
		for (JLabel l : cards){
			l.setTransferHandler( new ImageTransferHandler() );
			l.addMouseListener( this );
			l.setBounds(xpos[i], ypos[i++], l.getIcon().getIconWidth(), l.getIcon().getIconHeight() );
			add( l );
		}
		setVisible( true );		
	}
	
	/**
	 * actionPerformed - to handle button clicks by the user
	 */
	public void actionPerformed(ActionEvent arg0) {
		/*
		 * move the cards to the left-  the left-most card moves to the right end.
		 */
		Image leftmost = ( (ImageIcon) ( cards[0].getIcon() ) ).getImage();
		for (int i=0; i < cards.length-1; i++ ) {
			ImageIcon source      = (ImageIcon) ( cards[i+1].getIcon() );
			ImageIcon destination = (ImageIcon) ( cards[i]  .getIcon() );
			destination.setImage( source.getImage() );
		}
		// the left-most card moves to the right end.
		( (ImageIcon) (cards[ cards.length-1 ].getIcon()) ).setImage( leftmost );
			
		repaint();
	}

	public void paint(Graphics g){
		super.paint(g);
		
	}

	/**
	 * The next 5 methods are MouseListener methods
	 */
	public void mouseClicked(MouseEvent arg0) {
		System.out.println( "Mouse Clicked" );
	}

	public void mouseEntered(MouseEvent arg0) {
		System.out.println( "Mouse Entered" );
	}

	public void mouseExited(MouseEvent arg0) {
		System.out.println( "Mouse Exited" );
	}

	public void mousePressed(MouseEvent arg0) {
		System.out.println( "Mouse Pressed" );
		JLabel source = (JLabel) arg0.getSource();
		source.getTransferHandler().exportAsDrag( source, arg0, TransferHandler.MOVE );
	}

	public void mouseReleased(MouseEvent arg0) {
		System.out.println( "Mouse Released" );
	}

	
	/**
	 * main - the program starts here.
	 * 
	 * @param args
	 */
	public static void main(String[] args) {
		new CardGame();
	}
}
